/*
 * Created on Jan 7, 2005
 *
 * Copyright (c) Patrick Armstrong 2002-2007. All Rights Reserved
 */
package org.dime.mine.bot;

import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;

import org.dime.mine.game.Board;
import org.dime.util.Tuple;

/**
 * MineBot // TODO Add type summary
 *
 * @author Owner
 */
public class MineBot
{
    public final static Truth TRUTH = new Truth();
    public final static Contradiction CONTRADICTION = new Contradiction();
    public final static Exhaustion EXHAUSTION = new Exhaustion();
    public final static Burnout BURNOUT = new Burnout();
    public final static Guess GUESS = new Guess();

    private AwareBoard board;
    private List<Tuple<Strategy, Boolean>> strategies = new LinkedList<Tuple<Strategy, Boolean>>();

    public MineBot()
    {
        strategies.add(new Tuple<Strategy, Boolean>(TRUTH, true));
        strategies.add(new Tuple<Strategy, Boolean>(CONTRADICTION, true));
        strategies.add(new Tuple<Strategy, Boolean>(EXHAUSTION, true));
        strategies.add(new Tuple<Strategy, Boolean>(BURNOUT, true));
        strategies.add(new Tuple<Strategy, Boolean>(GUESS, true));
    }

    public MineBot(Board board)
    {
        this(new AwareBoard(board));
    }
    
    public MineBot(AwareBoard board)
    {
        this();
        setBoard(board);
    }
    
    public boolean next()
    {
        if (! board.isValid())
            return false;

        Iterator<Tuple<Strategy, Boolean>> it = strategies.iterator();
        Tuple<Strategy, Boolean> tuple;
        boolean flag = false;
        while (it.hasNext())
        {
            tuple = it.next();
            if(tuple.getTail())
                flag = tuple.getHead().execute(board);

            if (flag)
                return true;
        }
        return false;
    }
    public AwareBoard getBoard()
    {
        return board;
    }
    
    public void setBoard(Board board)
    {
        setBoard(new AwareBoard(board));
    }
    
    public void setBoard(AwareBoard board)
    {
        this.board = board;
    }

    public void setStrategyEnabled(Strategy strategy, boolean enabled)
    {
        getStrategyTuple(strategy).setTail(enabled);
    }
    
    public boolean isStrategyEnabled(Strategy strategy)
    {
        return getStrategyTuple(strategy).getTail();
    }
    
    private Tuple<Strategy, Boolean> getStrategyTuple(Strategy strategy)
    {
        Iterator<Tuple<Strategy, Boolean>> it = strategies.iterator();
        Tuple<Strategy, Boolean> tuple;
        while (it.hasNext())
        {
            tuple = it.next();
            if (tuple.getHead().equals(strategy))
                return tuple;
        }
        return null;
    }
}